package com.aca.market.controller;

import com.aca.market.entity.Indent;
import com.aca.market.entity.IndentItem;
import com.aca.market.service.IIndentItemService;
import com.aca.market.service.IIndentService;
import com.aca.market.service.IUserService;
import com.aca.market.util.UIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/indent")
@Slf4j
public class IndentController {

    @Autowired
    IIndentService indentService;
    @Autowired
    IUserService userService;
    @Autowired
    IIndentItemService iiService;


    @GetMapping("/deal-success")
    public String success(){
        return "success";
    }

    @GetMapping("/deal-failure")
    public String fail(){
        return "fail";
    }

    //后台部分

    @GetMapping("/detail")
    public String getIndent(@RequestParam Long indentId, Model model){
        Indent indent = indentService.getById(indentId);
//        userService.nowSession().setAttribute("indentId",indentId);
        //还要拿到订单对应的商品
        List<IndentItem> indentItems = iiService.list(
                new QueryWrapper<IndentItem>().eq("indent_id", indentId));
        log.info("Controller===========indentItems"+indentItems);
        model.addAttribute("items",indentItems);
        model.addAttribute("indentDetail",indent);
        return "indent-detail";
    }

    //测试用
//    @GetMapping("/detail/")
//    @ResponseBody
//    public Indent getIndent(@RequestParam Long indentId, Model model){
//        Indent indent = indentService.getById(indentId);
//        model.addAttribute("indentDetail"+indentId.toString(),indent);
//        return indent;
//    }

    @GetMapping("/list")
    public String listIndent(Model model){
        List<Indent> indents = indentService.list();
        model.addAttribute("indents",indents);
        return "indent-list";
    }

    //管理员加订单还蛮怪的

    @GetMapping("/toAdd")
    public String toAddIndent(){
        return "indent-add";
    }

    @PostMapping("/add")
    public String addIndent(@RequestBody Indent indent){
        Indent tmp = indent;
        LocalDateTime now = LocalDateTime.now();
        indent.setUpdateTime(now).setCreateTime(now).setEndTime(now.plusMinutes(15L));
        if (tmp.getShippingCode().isEmpty()){
            tmp.setShippingCode(UIDUtil.getUUID().substring(0,6));
        }else if (tmp.getShippingName().isEmpty()){
            tmp.setShippingName("默认快递");
        }
        log.info("==================Indent Add:" + tmp);
        indentService.save(tmp);
        return "redirect:/indent/list";
    }

    @GetMapping("/del")
    public String delIndent(@RequestParam("indentId") Long indentId){
        indentService.removeById(indentId);
        return "redirect:/indent/list";
    }

    //删多个
    @PostMapping("/delete/{indentIdList}")
    public String delIndent(@PathVariable("indentIdList") List<Long> indentIdList){
        indentService.removeByIds(indentIdList);
        return "redirect:/indent/list";
    }

    @PostMapping("/update")
    //改地址,改订单相商品相关内容还没做
    public String updateIndent(@RequestBody Indent indent){
        Indent tmp = indent;
        //更新时间
        tmp.setUpdateTime(LocalDateTime.now());
        log.info("updateIndent========="+tmp.toString());
        indentService.updateById(tmp);
        return "redirect:/indent/list";
    }

    @PostMapping("/update/itemQty/{itemId}")
    public Boolean updateItemQty(@PathVariable Long itemId
            ,@RequestParam Integer itemQty){
        return iiService.update(new UpdateWrapper<IndentItem>().eq("item_id",itemId).set("count",itemQty));
    }

}
